//
//  RBCompeRecomColleViewCell.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/12.
//

#import "RBCompeRecomColleViewCell.h"
#import "RBCompeRecommendModel.h"

@interface RBCompeRecomColleViewCell ()
@property(strong,nonatomic)UIView *bgView;
@property (nonatomic,strong) UIView *topView;
@property(strong,nonatomic)UILabel *typeLabel;
@property(strong,nonatomic)UILabel *timeLabel;
@property(strong,nonatomic)UIImageView *homeLogoImageView;
@property(strong,nonatomic)UILabel *homeNameLabel;
@property(strong,nonatomic)UILabel *homeCountLabel;
@property(strong,nonatomic)UIImageView *visLogoImageView;
@property(strong,nonatomic)UILabel *visNameLabel;
@property(strong,nonatomic)UILabel *visCountLabel;
@property(strong,nonatomic)UILabel *vsLabel;
@property(strong,nonatomic)UIView *bottomView;
@property(strong,nonatomic)UIImageView *bottomBgImageView;
@property(strong,nonatomic)UILabel *countOptionLabel;
@property(strong,nonatomic)UILabel *statusLabel;
@end

@implementation RBCompeRecomColleViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self rb_initViewUI];
        [self setNeedsLayout];
    }
    return self;
}
-(void)rb_initViewUI{
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.topView];;
    [self.topView addSubview:self.typeLabel];
    [self.topView addSubview:self.timeLabel];
    [self.bgView addSubview:self.homeLogoImageView];
    [self.bgView addSubview:self.homeNameLabel];
    [self.bgView addSubview:self.homeCountLabel];
    [self.bgView addSubview:self.visLogoImageView];
    [self.bgView addSubview:self.visNameLabel];
    [self.bgView addSubview:self.visCountLabel];
    [self.bgView addSubview:self.vsLabel];
    [self.bgView addSubview:self.bottomView];
    [self.bottomView addSubview:self.bottomBgImageView];
    [self.bottomView addSubview:self.countOptionLabel];
    [self.bottomView addSubview:self.statusLabel];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.mas_equalTo(0);
    }];
    self.bgView.layer.cornerRadius = 5;
    self.bgView.layer.masksToBounds = YES;
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(RBScaleWidth(30));
    }];
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(RBScaleWidth(10));
        make.top.mas_equalTo(RBScaleWidth(10));
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-RBScaleWidth(10));
        make.centerY.mas_equalTo(self.typeLabel.mas_centerY);
    }];
    [self.homeLogoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topView.mas_bottom);
        make.left.mas_equalTo(RBScaleWidth(10));
        make.width.height.mas_equalTo(RBScaleWidth(18));
    }];
    self.homeLogoImageView.clipsToBounds = YES;
    [self.homeNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.homeLogoImageView.mas_right).mas_offset(RBScaleWidth(10));
        make.centerY.mas_equalTo(self.homeLogoImageView.mas_centerY);
    }];
    [self.homeCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-RBScaleWidth(10));
        make.centerY.mas_equalTo(self.homeLogoImageView.mas_centerY);
    }];
    [self.visLogoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.homeLogoImageView.mas_bottom).mas_offset(RBScaleWidth(9));
        make.left.mas_equalTo(self.homeLogoImageView.mas_left);
        make.width.height.mas_equalTo(RBScaleWidth(18));
    }];
    self.visLogoImageView.clipsToBounds = YES;
    [self.visNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.visLogoImageView.mas_right).mas_offset(RBScaleWidth(10));
        make.centerY.mas_equalTo(self.visLogoImageView.mas_centerY);
    }];
    [self.visCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-RBScaleWidth(10));
        make.centerY.mas_equalTo(self.visLogoImageView.mas_centerY);
    }];
    [self.vsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-RBScaleWidth(10));
        make.centerY.mas_equalTo(0);
    }];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(RBScaleWidth(20));
    }];
    [self.bottomBgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-RBScaleWidth(10));
        make.centerY.mas_equalTo(0);
    }];
    [self.countOptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(RBScaleWidth(10));
        make.centerY.mas_equalTo(0);
    }];

}
-(void)setModel:(RBCompeRecommendModel *)model{
    _model=model;
    self.typeLabel.text = model.competitionName;

    if(RBStringIsEmpty(model.articleCount)){
        model.articleCount = @"0";
    }
    self.countOptionLabel.text = [NSString stringWithFormat:@"%@个方案",model.articleCount];
    NSString *stauesLabStr = @"";
    if ([model.sportId integerValue] == 1) {//足球
        if ([model.statusId intValue] == 2 ||[model.statusId intValue] == 3 || [model.statusId intValue] == 4||[model.statusId intValue] == 5 ||[model.statusId intValue] == 6 ||[model.statusId intValue] == 7) {
            stauesLabStr = @"进行中";
        }
       
        if ([model.statusId intValue] == 1) {
            stauesLabStr = @"未开赛";
        }
        
        if ([model.statusId intValue] == 8) {
            stauesLabStr = @"完场";
        }
        
        if ([model.statusId intValue] == 9) {
            stauesLabStr = @"推迟";
        }
        
        if ([model.statusId intValue] == 10) {
            stauesLabStr = @"中断";
        }
        
        if ([model.statusId intValue] == 11) {
            stauesLabStr = @"腰斩";
        }
        
        if ([model.statusId intValue] == 12) {
            stauesLabStr =@"取消";
        }
        
        if ([model.statusId intValue] == 13) {
            stauesLabStr =@"待定";
        }
            
    }else{
        switch ([model.statusId intValue]) {
            case 1:
                stauesLabStr = @" 未开赛 " ;
                break;
            case 2:
                stauesLabStr = @" 进行中 ";
                break;
            case 3:
                stauesLabStr = @"进行中";
                break;
            case 4:
                stauesLabStr = @" 进行中 ";
                break;
            case 5:
                stauesLabStr = @" 进行中 ";
                break;
            case 6:
                stauesLabStr = @" 进行中 ";
                break;
            case 7:
                stauesLabStr = @" 进行中 ";
                break;
            case 8:
                stauesLabStr = @" 进行中 ";
                break;
            case 9:
                stauesLabStr = @" 进行中 ";
                break;
            case 10:
                stauesLabStr = @" 完场 ";
                break;
                
            case 11:
                stauesLabStr = @" 中断 ";
                break;
            case 12:
                stauesLabStr = @" 取消 ";
                break;
            case 13:
                stauesLabStr = @" 延期 ";
                break;
            case 14:
                stauesLabStr = @" 腰斩 ";
                break;
            case 15:
                stauesLabStr = @" 待定 ";
                break;
            default:
                stauesLabStr = @" 比赛异常 ";
                break;
        }
    }
    self.statusLabel.text = stauesLabStr;

    NSString *hourStr = model.matchTime.rb_format_HH_mm;
    self.timeLabel.text = [NSString stringWithFormat:@"%@",hourStr];
    
    NSString *homeLogo;
    NSString *awayLogo;
    NSString *homeNameZh;
    NSString *awayNameZh;
    NSString *homeTitle;
    NSString *awayTitle;
    if ([model.sportId intValue] == 2) { //篮球
        NSInteger homeScore = [[[model.homeScores mj_JSONObject] valueForKeyPath:@"@sum.floatValue"] integerValue];
        NSInteger awayScore = [[[model.awayScores mj_JSONObject] valueForKeyPath:@"@sum.floatValue"] integerValue];
        self.homeCountLabel.text = [NSString stringWithFormat:@"%ld",homeScore];
        self.visCountLabel.text = [NSString stringWithFormat:@"%ld",awayScore];
        
        homeLogo = model.awayLogo;
        awayLogo = model.homeLogo;
        homeNameZh = model.awayNameZh;
        awayNameZh = model.homeNameZh;
        homeTitle = @" [客]";
        awayTitle = @" [主]";
    }else{
        NSArray *homeSrcoreArr = [model.homeScores mj_JSONObject];
        NSArray *awayeSrcoreArr = [model.awayScores mj_JSONObject];
        if (homeSrcoreArr.count == 7) {
            NSInteger homeScore = [homeSrcoreArr[0] integerValue];
            NSInteger awayScore = [awayeSrcoreArr[0] integerValue];
            self.homeCountLabel.text = [NSString stringWithFormat:@"%ld",homeScore];
            self.visCountLabel.text = [NSString stringWithFormat:@"%ld",awayScore];
        }
        homeLogo = model.homeLogo;
        awayLogo = model.awayLogo;
        homeNameZh = model.homeNameZh;
        awayNameZh = model.awayNameZh;
        homeTitle = @" [主]";
        awayTitle = @" [客]";
    }
    [self.homeLogoImageView sd_setImageWithURL:[NSURL URLWithString:homeLogo] placeholderImage:[UIImage imageNamed:@""]];
    [self.visLogoImageView sd_setImageWithURL:[NSURL URLWithString:awayLogo] placeholderImage:[UIImage imageNamed:@""]];
    if(RBStringIsEmpty(homeNameZh)){
        homeNameZh = @"";
    }
    NSArray *texts = @[homeNameZh,homeTitle];
    NSArray *colors = @[RBColor(51, 51, 51),RBColor(153, 153, 153)];
    NSArray *fonts = @[RBPingFangMediumFont(12), RBPingFangFont(10)];
    NSAttributedString *homeAttr = [UILabel rb_attributedTexts:texts textColors:colors textFonts:fonts lineSpacing:0.0];
    self.homeNameLabel.attributedText = homeAttr;

    if(RBStringIsEmpty(awayNameZh)){
        awayNameZh = @"";
    }
    NSArray *vistexts = @[awayNameZh,awayTitle];
    NSArray *viscolors = @[RBColor(51, 51, 51),RBColor(153, 153, 153)];
    NSArray *visfonts = @[RBPingFangMediumFont(12), RBPingFangFont(10)];
    NSAttributedString *visAttr = [UILabel rb_attributedTexts:vistexts textColors:viscolors textFonts:visfonts lineSpacing:0.0];
    self.visNameLabel.attributedText = visAttr;
    if([self.statusLabel.text  containsString:@"进行中"]){
        self.vsLabel.hidden = YES;
    }else{
        self.vsLabel.hidden = NO;
    }
    if([self.statusLabel.text containsString:@"进行中"] || [self.statusLabel.text containsString:@"完场"]){
        self.homeCountLabel.hidden = NO;
        self.visCountLabel.hidden = NO;
    }else{
        self.homeCountLabel.hidden = YES;
        self.visCountLabel.hidden = YES;
    }
    
    [self setNeedsLayout];
}
- (UIView *)bgView{
    if(_bgView==nil){
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}
- (UIView *)topView{
    if(_topView==nil){
        _topView = [[UIView alloc] init];
    }
    return _topView;
}
-(UILabel *)typeLabel{
    if(_typeLabel==nil){
        _typeLabel = [UILabel rb_labelWithTitle:@"解放者杯" color:RBColor(153, 153, 153) font:RBPingFangFont(11) alignment:NSTextAlignmentLeft];
    }
    return _typeLabel;
}
-(UILabel *)timeLabel{
    if(_timeLabel==nil){
        _timeLabel = [UILabel rb_labelWithTitle:@"11:00" color:RBColor(153, 153, 153) font:RBPingFangFont(11) alignment:NSTextAlignmentLeft];
    }
    return _timeLabel;
}
- (UIImageView *)homeLogoImageView{
    if(_homeLogoImageView==nil){
        _homeLogoImageView = [[UIImageView alloc] init];
        _homeLogoImageView.image = [UIImage imageNamed:@""];
        _homeLogoImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _homeLogoImageView;
}

-(UILabel *)homeNameLabel{
    if(_homeNameLabel==nil){
        _homeNameLabel = [UILabel rb_labelWithTitle:@"毕尔巴鄂" color:RBColor(51, 51, 51) font:RBPingFangMediumFont(12) alignment:NSTextAlignmentLeft];
    }
    return _homeNameLabel;
}
-(UILabel *)homeCountLabel{
    if(_homeCountLabel==nil){
        _homeCountLabel = [UILabel rb_labelWithTitle:@"0" color:RBColor(242, 48, 48) font:RBPingFangHeavyFont(13) alignment:NSTextAlignmentRight];
    }
    return _homeCountLabel;
}
- (UIImageView *)visLogoImageView{
    if(_visLogoImageView==nil){
        _visLogoImageView = [[UIImageView alloc] init];
        _visLogoImageView.image = [UIImage imageNamed:@""];
        _visLogoImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _visLogoImageView;
}

-(UILabel *)visNameLabel{
    if(_visNameLabel==nil){
        _visNameLabel = [UILabel rb_labelWithTitle:@"毕尔巴鄂" color:RBColor(51, 51, 51) font:RBPingFangMediumFont(12) alignment:NSTextAlignmentLeft];
    }
    return _visNameLabel;
}
-(UILabel *)visCountLabel{
    if(_visCountLabel==nil){
        _visCountLabel = [UILabel rb_labelWithTitle:@"0" color:RBColor(242, 48, 48) font:RBPingFangHeavyFont(13) alignment:NSTextAlignmentRight];
    }
    return _visCountLabel;
}
-(UILabel *)vsLabel{
    if(_vsLabel==nil){
        _vsLabel = [UILabel rb_labelWithTitle:@"VS" color:RBColor(242, 48, 48) font:RBPingFangHeavyFont(13) alignment:NSTextAlignmentRight];
    }
    return _vsLabel;
}
- (UIView *)bottomView{
    if(_bottomView==nil){
        _bottomView = [[UIView alloc] init];
    }
    return _bottomView;
}
- (UIImageView *)bottomBgImageView{
    if(_bottomBgImageView==nil){
        _bottomBgImageView = [[UIImageView alloc] init];
        _bottomBgImageView.image = [UIImage imageNamed:@"bst"];
        _bottomBgImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _bottomBgImageView;
}
-(UILabel *)countOptionLabel{
    if(_countOptionLabel==nil){
        _countOptionLabel = [UILabel rb_labelWithTitle:@"0个方案" color:RBColor(255, 255, 255) font:RBPingFangMediumFont(11) alignment:NSTextAlignmentLeft];
    }
    return _countOptionLabel;
}
-(UILabel *)statusLabel{
    if(_statusLabel==nil){
        _statusLabel = [UILabel rb_labelWithTitle:@"未开赛" color:RBColor(255, 255, 255) font:RBPingFangMediumFont(11) alignment:NSTextAlignmentRight];
    }
    return _statusLabel;
}
@end
